package internal

import (
	"fmt"
	consulapi "github.com/hashicorp/consul/api"
)

const (
	ConsulAddress = "192.168.75.6:8500"
	LocalIP       = "192.168.1.99"
	LocalPort     = 8005
)

func GetServiceList() error {
	defaultConfig := consulapi.DefaultConfig()
	h := AppConf.ConsulConfig.Host
	p := AppConf.ConsulConfig.Port
	defaultConfig.Address = fmt.Sprintf("%s:%d", h, p)
	client, err := consulapi.NewClient(defaultConfig)
	if err != nil {
		return err
	}
	servicesList, err := client.Agent().Services()
	if err != nil {
		return err
	}
	for k, v := range servicesList {
		fmt.Println(k)
		fmt.Println(v)
		fmt.Println("-------------------------------------")
	}
	return nil
}

func FilterService() error {
	defaultConfig := consulapi.DefaultConfig()
	h := AppConf.ConsulConfig.Host
	p := AppConf.ConsulConfig.Port
	defaultConfig.Address = fmt.Sprintf("%s:%d", h, p)
	client, err := consulapi.NewClient(defaultConfig)
	if err != nil {
		return err
	}
	serviceList, err := client.Agent().ServicesWithFilter("service==account_web")
	if err != nil {
		return err
	}
	for k, v := range serviceList {
		fmt.Println(k)
		fmt.Println(v)
		fmt.Println("----------------------------------------------------")
	}
	return nil
}
